package com.zhou.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zhou.gulimall.product.entity.AttrEntity;
import com.zhou.gulimall.product.service.AttrAttrgroupRelationService;
import com.zhou.gulimall.product.service.AttrService;
import com.zhou.gulimall.product.vo.WithatteVo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.common.utils.PageUtils;
import com.zhou.common.utils.Query;

import com.zhou.gulimall.product.dao.AttrGroupDao;
import com.zhou.gulimall.product.entity.AttrGroupEntity;
import com.zhou.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

@Slf4j
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //SELECT * FROM pms_attr_group WHERE catelog_id = ? AND (attr_group_id = KEY OR attr_group_name LIKE %KEY%)
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if (catelogId == 0){
            IPage<AttrGroupEntity> page =this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }else {
            queryWrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                   queryWrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<WithatteVo> getWithatteList(Long catelogId) {
        /**
         * 1.根据接受的catelogId在pms_attr_group表中获取到对应的attr_group_id(一对多)
         * 将查到的attrGroupEntity对象属性赋值给返回对象WithatteVo
         * 2.根据获取的attr_group_id在pms_attr_attrgroup_relation表中获取对应的attr_id(一对多)
         * 3.将获取的attr_id在pms_attr表中获取对应的attrEntity对象
         */
        List<WithatteVo> withatteVos = new ArrayList<>();
        List<AttrGroupEntity> list = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        for (int i = 0; i < list.size(); i++) {
            WithatteVo withatteVo = new WithatteVo();
           BeanUtils.copyProperties(list.get(i),withatteVo);
           Long groupId = list.get(i).getAttrGroupId();
           List<AttrEntity> attrEntityList = new ArrayList<>();
           List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, groupId));
            for (int j = 0; j < attrgroupRelationEntityList.size(); j++) {
              Long attrId =  attrgroupRelationEntityList.get(j).getAttrId();
              attrEntityList.add(attrService.getById(attrId));
            }
           withatteVo.setAttrs(attrEntityList);

            withatteVos.add(withatteVo);
        }

        return withatteVos;
    }

}